//
//  CustomPickerPopView.swift
//  EasePods
//
//  Created by 易先生 on 2024/5/7.
//

import UIKit
import SwiftEntryKit

class CustomPickerPopView: View {

    private var dataSource: [String] = []

    private var selectIndex: Int = -1
    
    public var confirmBlock : ((Int) -> Void)?
    
    override func makeUI() {
        super.makeUI()
        
        snp.makeConstraints { make in
            make.width.equalTo(kScreenWidth)
        }
        
        self.addSubview(titleLabel)
        self.addSubview(closeButton)
        self.addSubview(pickerView)
        self.addSubview(confirmButton)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(30)
            make.leading.equalToSuperview().offset(20)
        }
        
        closeButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-20)
            make.centerY.equalTo(titleLabel)
        }
        
        pickerView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(42)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.height.equalTo(178)
        }
        
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(pickerView.snp.bottom).offset(30)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.height.equalTo(57)
            make.bottom.equalToSuperview().offset(-50)
        }
    }
    
    @objc private func click_confirm() {
        confirmBlock?(selectIndex)
    }
    
    @objc private func click_close() {
        SwiftEntryKit.dismiss()
    }
    
    public func bind(to datas: [String], title: String, selectIndex: Int) {
        self.titleLabel.text = title
        self.selectIndex = selectIndex
        dataSource = datas
        self.pickerView.selectRow(selectIndex, inComponent: 0, animated: true)
        self.pickerView.reloadComponent(0)
    }
    
    private lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 16)
        label.textColor = R.color.common_3_text()
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    private lazy var closeButton: UIButton = {
        let button = UIButton()
        button.setImage(R.image.ic_common_popup(), for: .normal)
        button.addTarget(self, action: #selector(click_close), for: .touchUpInside)
        return button
    }()
    
    private lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle(R.string.localizable.common_confirm(), for: .normal)
        button.setTitleColor(R.color.common_24_button_text(), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.titleLabel?.numberOfLines = 0
        button.backgroundColor = R.color.common_22_button_n()
        button.layer.cornerRadius = 6
        button.addTarget(self, action: #selector(click_confirm), for: .touchUpInside)
        return button
    }()
    
    private lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        return pickerView
    }()
}

extension CustomPickerPopView: UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataSource.count
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        var label: UILabel
        if let view = view as? UILabel {
            label = view
        } else {
            label = UILabel()
        }
        
        label.textAlignment = .center
        label.font = Fonts.systemFont(ofSize: 16)
        label.text = dataSource[row]
        label.textColor = R.color.common_31_popup_text_h()
        
        return label
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    
//    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
//        
//        let content = dataSource[row]
//        let color: UIColor? = self.selectIndex == row ? R.color.common_31_popup_text_h() : R.color.common_32_popup_text_n()
//        let font: UIFont = self.selectIndex == row ? Fonts.systemFont(ofSize: 16) : Fonts.systemFont(ofSize: 15)
//        let attributedString = NSMutableAttributedString(string: content)
//        let str = NSString(string: content)
//        let theRange = str.range(of: content)
//        attributedString.addAttribute(.foregroundColor, value: color ?? UIColor.black, range: theRange)
//        attributedString.addAttribute(.font, value: font, range: theRange)
//        return attributedString
//    }
    
    
}

extension CustomPickerPopView: UIPickerViewDelegate {
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectIndex  = row
        pickerView.reloadAllComponents()
    }
}
